<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="岗位负责人" prop="jobName">
        <el-input
            v-model="queryParams.jobName"
            placeholder="请输入负责人姓名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Download"
            @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="岗位编号" fixed align="center" width="100" prop="jobId"/>
      <el-table-column label="岗位负责人" align="center" prop="jobType">
        <template #default="scope">
          <span v-if="scope.row.jobType==0">内部岗</span>
          <span v-else>外部岗</span>
        </template>
      </el-table-column>
      <el-table-column label="岗位负责人" align="center" prop="jobName"/>
      <el-table-column label="电话" align="center" prop="jobPhone"/>
      <el-table-column label="地址" align="center" prop="jobPosition"/>
      <el-table-column label="可完成的实验和报价" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="showCanDo(scope.row.jobId)"
                     >查看
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="jobStatus">
        <template #default="scope">
          <el-switch v-model="scope.row.jobStatus" :active-value="1" :inactive-value="0" @change="changeStatus(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     >修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改实验岗位管理对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body style=" border-radius: 20px;" class="larger-input">
      <el-form ref="experimentRef" :model="form" :rules="postRules" label-width="100px">
        <el-form-item label="岗位负责人" prop="jobName">
          <el-input v-model="form.jobName" placeholder="请输入岗位负责人"/>
        </el-form-item>
        <el-form-item label="电话" prop="jobPhone">
          <el-input v-model="form.jobPhone" placeholder="请输入电话"/>
        </el-form-item>
        <el-form-item label="地址" prop="jobPosition">
          <el-input v-model="form.jobPosition" type="textarea" rows="3" style="width: 100%" placeholder="请输入地址"/>
        </el-form-item>
        <el-form-item label="岗位类型">
          <el-switch
              v-model="form.jobType"
              size="large"
              active-text="外部岗"
              :active-value="1"
              inactive-text="内部岗"
              :inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog v-model="openCanDoList" width="800px" append-to-body style=" border-radius: 20px;">
      <el-button type="primary" @click="addCanDo1">添加</el-button>
      <el-table v-loading="loading" :data="canDoList">
        <el-table-column label="可完成实验" prop="experimentType"/>
        <el-table-column label="报价" prop="price"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate2(scope.row.id,scope.row.jobId)"
                       >修改
            </el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete2(scope.row.id,scope.row.jobId)"
                       >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-dialog v-model="openCanDo" width="500px" append-to-body style=" border-radius: 20px;" class="larger-input">
      <el-form ref="canDoRef" :model="etform" :rules="expeRules" label-width="100px">
        <el-form-item label="可完成实验" prop="experimentTypeId">
          <el-select
              v-model="etform.experimentTypeId"
              filterable
              placeholder="Select"
              style="width: 500px;max-height: 100px">
            <el-option
                v-for="item in experimentTypeList"
                :key="item.typeId"
                :label="item.typeName"
                :value="item.typeId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报价" prop="price">
          <el-input v-model="etform.price" placeholder="请输入报价"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm2">确 定</el-button>
          <el-button @click="cancel2">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Experiment">
import {
  addCanDo,
  addInformation, deleteCando,
  delInformation, getCanDo,
  getInformation, listCanDo,
  listInformation, updateCanDo,
  updateInformation
} from "@/api/post/experiment.js";
import {listAllExperiment} from "@/api/experiment/experiment.js";

const {proxy} = getCurrentInstance();

const postList = ref([]);
const canDoList = ref([]);
const canDoSet = new Set;
const experimentTypeList = ref([]);

const open = ref(false);
const openCanDo = ref(false);
const openCanDoList = ref(false);

const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  etform: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    jobName: null
  },
  postRules: {
    jobName:[
      {required:true, message: '请填写岗位负责人姓名', trigger: 'blur' }
    ]
  },
  expeRules:{
    experimentTypeId:[
      {required:true, message: '请选择可完成的实验', trigger: 'blur' },
    ],
    price:[
      {required:true, message: '请填写报价', trigger: 'blur' }
    ]
  }
});

const {queryParams, form, postRules,expeRules, etform} = toRefs(data);

/** 查询实验类型管理列表 */
function getList() {
  loading.value = true;
  listInformation(queryParams.value).then(response => {
    postList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    jobId: null,
    jobName: null,
    jobPhone: null,
    jobPosition: null,
    jobStatus: null,
    jobType:0
  };
  proxy.resetForm("experimentRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.jobId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加实验岗位";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _typeId = row.jobId || ids.value
  getInformation(_typeId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改实验岗位";
  });
}

/** 提交按钮 */
function submitForm() {
  console.log(form.value)
  proxy.$refs["experimentRef"].validate(valid => {
    if (valid) {
      if (form.value.jobId != null) {
        updateInformation(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInformation(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _typeIds = row.jobId || ids.value;
  proxy.$modal.confirm('是否确认删除岗位编号为"' + _typeIds + '"的数据项？').then(function () {
    return delInformation(_typeIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('post/experiment/export', {
    ...queryParams.value
  }, `experiment_${new Date().getTime()}.xlsx`)
}

//
function addCanDo1() {
  openCanDo.value = true;
  etform.value.experimentTypeId=null;
  etform.value.price=null;
  etform.value.id=null;
  //查询实验类型
  listAllExperiment().then(res => {
    experimentTypeList.value = []
    for (let row of res.rows) {
      if (!canDoSet.has(row.typeId)) {
        experimentTypeList.value.push(row);
      }
    }
  })
}

function submitForm2() {
  proxy.$refs["canDoRef"].validate(valid => {
    if (valid) {
      if (etform.value.id != null) {
        updateCanDo(etform.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          openCanDo.value = false;
          showCanDo(etform.value.jobId);
        });
      } else {
        addCanDo(etform.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          openCanDo.value = false;
          showCanDo(etform.value.jobId);
        });
      }
    }
  });
}

function cancel2() {
  openCanDo.value = false;
}

function showCanDo(jobId) {
  //查找该id下可做实验
  canDoSet.clear();
  etform.value.jobId=jobId;
  listCanDo(jobId).then(res=>{
    canDoList.value=res.rows;
    canDoList.value.forEach(e=>{canDoSet.add(e.experimentTypeId)})
  })
  openCanDoList.value = true;
}
function handleUpdate2(id,jobId){
  getCanDo(id).then(res=>{
    etform.value=res.data;
    openCanDo.value=true;
  })
}
function handleDelete2(id,jobId){
  proxy.$modal.confirm('是否确认删除编号为"' + id + '"的数据项？').then(function () {
    return deleteCando(id);
  }).then(() => {
    showCanDo(jobId);
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

function changeStatus(row){
  let t={
    jobId:row.jobId,
    jobStatus:row.jobStatus?1:0
  }
  updateInformation(t).then(response => {
    getList();
  });
}

getList();
</script>
